<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <title>用&ldquo;自然排序&rdquo;算法对数组排序</title>
 </head>
 <body class="docs"><div id="layout">
  <div id="layout-content"><div id="function.natsort" class="refentry">
   <div class="refnamediv">
    <h1 class="refname">natsort</h1>
    <p class="verinfo">(PHP 4, PHP 5, PHP 7)</p><p class="refpurpose"><span class="refname">natsort</span> &mdash; <span class="dc-title">
     用“自然排序”算法对数组排序
    </span></p>

   </div>
 <div class="refsect1 description" id="refsect1-function.natsort-description">
  <h3 class="title">说明</h3>
     <div class="methodsynopsis dc-description">
      <span class="methodname"><strong>natsort</strong></span>
       ( <span class="methodparam"><span class="type">array</span> <code class="parameter reference">&$array</code></span>
      ) : <span class="type">bool</span></div>

    <p class="para rdfs-comment">
     本函数实现了一个和人们通常对字母数字字符串进行排序的方法一样的排序算法并保持原有键／值的关联，这被称为“自然排序”。本算法和通常的计算机字符串排序算法（用于
     <span class="function"><a href="sort.html" class="function">sort()</a></span>）的区别见下面示例。
    </p>
  <blockquote class="note"><p><strong class="note">Note</strong>: 
 <p class="para">
  If two members compare as equal, their relative order in the sorted array is undefined.
 </p>
</p></blockquote>

   </div>

 <div class="refsect1 parameters" id="refsect1-function.natsort-parameters">
  <h3 class="title">参数</h3>
  <p class="para">
   <dl>

    
     <dt>
<code class="parameter">array</code></dt>

     <dd>

      <p class="para">
       输入的 array。
      </p>
     </dd>

    
   </dl>

  </p>
 </div>

 <div class="refsect1 returnvalues" id="refsect1-function.natsort-returnvalues">
  <h3 class="title">返回值</h3>
  <p class="para">
   成功时返回 <strong><code>TRUE</code></strong>， 或者在失败时返回 <strong><code>FALSE</code></strong>。
  </p>
 </div>


 <div class="refsect1 changelog" id="refsect1-function.natsort-changelog">
  <h3 class="title">更新日志</h3>
  <table class="doctable informaltable">
   
    <thead>
     <tr>
      <th>版本</th>
      <th>说明</th>
     </tr>

    </thead>

    <tbody class="tbody">
     <tr>
      <td>5.2.10</td>
      <td>
       用零填充的数字字符串 (例如 &#039;00005&#039;)现在本质上会忽略掉填充的前道零。
      </td>
     </tr>

    </tbody>
   
  </table>

 </div>


 <div class="refsect1 examples" id="refsect1-function.natsort-examples">
  <h3 class="title">范例</h3>
  <p class="para">
   <div class="example" id="example-6110">
    <p><strong>Example #1 <span class="function"><strong>natsort()</strong></span> 基本用法的操作示例</strong></p>
    <div class="example-contents">
<div class="phpcode"><pre><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br />$array1&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">$array2&nbsp;</span><span style="color: #007700">=&nbsp;array(</span><span style="color: #DD0000">"img12.png"</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">"img10.png"</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">"img2.png"</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">"img1.png"</span><span style="color: #007700">);<br /><br /></span><span style="color: #0000BB">asort</span><span style="color: #007700">(</span><span style="color: #0000BB">$array1</span><span style="color: #007700">);<br />echo&nbsp;</span><span style="color: #DD0000">"Standard&nbsp;sorting\n"</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">print_r</span><span style="color: #007700">(</span><span style="color: #0000BB">$array1</span><span style="color: #007700">);<br /><br /></span><span style="color: #0000BB">natsort</span><span style="color: #007700">(</span><span style="color: #0000BB">$array2</span><span style="color: #007700">);<br />echo&nbsp;</span><span style="color: #DD0000">"\nNatural&nbsp;order&nbsp;sorting\n"</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">print_r</span><span style="color: #007700">(</span><span style="color: #0000BB">$array2</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</pre></div>
    </div>

    <div class="example-contents"><p>以上例程会输出：</p></div>
    <div class="example-contents screen">
<div class="cdata"><pre>
Standard sorting
Array
(
    [3] =&gt; img1.png
    [1] =&gt; img10.png
    [0] =&gt; img12.png
    [2] =&gt; img2.png
)

Natural order sorting
Array
(
    [3] =&gt; img1.png
    [2] =&gt; img2.png
    [1] =&gt; img10.png
    [0] =&gt; img12.png
)
</pre></div>
    </div>
    <div class="example-contents"><p>
     For more information see: Martin Pool&#039;s <a href="http://sourcefrog.net/projects/natsort/" class="link external">&raquo;&nbsp;Natural Order String Comparison</a>
     page.
    </p></div>
   </div>
   <div class="example" id="example-6111">
    <p><strong>Example #2 <span class="function"><strong>natsort()</strong></span> examples demonstrating potential gotchas</strong></p>
    <div class="example-contents">
<div class="phpcode"><pre><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br /></span><span style="color: #007700">echo&nbsp;</span><span style="color: #DD0000">"Negative&nbsp;numbers\n"</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$negative&nbsp;</span><span style="color: #007700">=&nbsp;array(</span><span style="color: #DD0000">'-5'</span><span style="color: #007700">,</span><span style="color: #DD0000">'3'</span><span style="color: #007700">,</span><span style="color: #DD0000">'-2'</span><span style="color: #007700">,</span><span style="color: #DD0000">'0'</span><span style="color: #007700">,</span><span style="color: #DD0000">'-1000'</span><span style="color: #007700">,</span><span style="color: #DD0000">'9'</span><span style="color: #007700">,</span><span style="color: #DD0000">'1'</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">print_r</span><span style="color: #007700">(</span><span style="color: #0000BB">$negative</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">natsort</span><span style="color: #007700">(</span><span style="color: #0000BB">$negative</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">print_r</span><span style="color: #007700">(</span><span style="color: #0000BB">$negative</span><span style="color: #007700">);<br /><br />echo&nbsp;</span><span style="color: #DD0000">"Zero&nbsp;padding\n"</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$zeros&nbsp;</span><span style="color: #007700">=&nbsp;array(</span><span style="color: #DD0000">'09'</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'8'</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'10'</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'009'</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'011'</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'0'</span><span style="color: #007700">);&nbsp;<br /></span><span style="color: #0000BB">print_r</span><span style="color: #007700">(</span><span style="color: #0000BB">$zeros</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">natsort</span><span style="color: #007700">(</span><span style="color: #0000BB">$zeros</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">print_r</span><span style="color: #007700">(</span><span style="color: #0000BB">$zeros</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</pre></div>
    </div>

    <div class="example-contents"><p>以上例程会输出：</p></div>
    <div class="example-contents screen">
<div class="cdata"><pre>
Negative numbers
Array
(
    [0] =&gt; -5
    [1] =&gt; 3
    [2] =&gt; -2
    [3] =&gt; 0
    [4] =&gt; -1000
    [5] =&gt; 9
    [6] =&gt; 1
)
Array
(
    [2] =&gt; -2
    [0] =&gt; -5
    [4] =&gt; -1000
    [3] =&gt; 0
    [6] =&gt; 1
    [1] =&gt; 3
    [5] =&gt; 9
)

Zero padding
Array
(
    [0] =&gt; 09
    [1] =&gt; 8
    [2] =&gt; 10
    [3] =&gt; 009
    [4] =&gt; 011
    [5] =&gt; 0
)
Array
(
    [5] =&gt; 0
    [1] =&gt; 8
    [3] =&gt; 009
    [0] =&gt; 09
    [2] =&gt; 10
    [4] =&gt; 011
)
</pre></div>
    </div>
   </div>
  </p>
 </div>

 <div class="refsect1 seealso" id="refsect1-function.natsort-seealso">
  <h3 class="title">参见</h3>
  <p class="para">
   <ul class="simplelist">
    <li class="member"><span class="function"><a href="natcasesort.html" class="function" rel="rdfs-seeAlso">natcasesort()</a> - 用&ldquo;自然排序&rdquo;算法对数组进行不区分大小写字母的排序</span></li>
    <li class="member"><a href="array.sorting.html" class="link">数组排序函数对比</a></li>
    <li class="member"><span class="function"><a href="strnatcmp.html" class="function" rel="rdfs-seeAlso">strnatcmp()</a> - 使用自然排序算法比较字符串</span></li>
    <li class="member"><span class="function"><a href="strnatcasecmp.html" class="function" rel="rdfs-seeAlso">strnatcasecmp()</a> - 使用&ldquo;自然顺序&rdquo;算法比较字符串（不区分大小写）</span></li>
   </ul>
  </p>
 </div>

  </div></div></div></body></html>